package ym.ecrm.core.common.util;

import java.util.regex.Pattern;

import ym.ecrm.core.common.exception.ValidationException;

public class DataValidation {

	/**
	 * 验证是否为电话号码，包括：手机号码和固定电话 注意：为null或字符串""都返回true
	 * 
	 * @param value
	 * @return
	 * @throws ValidationException
	 */
	public static boolean isPhoneNumber(String value) {
		return isPhoneNumber(value, "电话号码格式不正确");
	}

	/**
	 * 验证是否为电话号码，包括：手机号码和固定电话 注意：为null或字符串""都返回true
	 * 
	 * @param value
	 * @return
	 * @throws ValidationException
	 */
	public static boolean isPhoneNumber(String value, String msg) {
		if (value == null || value.equals(""))
			return true;
		String regex = "(^(\\d{3,4}-)?\\d{7,8})$|(13[0-9]{9})";
		Pattern p = Pattern.compile(regex);
		if (p.matcher(value).matches()) {
			return true;
		} else {
			throw new ValidationException(msg);
		}
	}

	/**
	 * 验证是否为电子邮件地址 注意：为null或字符串""都返回true
	 * 
	 * @param value
	 * @return
	 * @throws ValidationException
	 */
	public static boolean isEmail(String value) {
		return isEmail(value, "电子邮件地址格式不正确");
	}

	/**
	 * 验证是否为电子邮件地址 注意：为null或字符串""都返回true
	 * 
	 * @param value
	 * @return
	 * @throws ValidationException
	 */
	public static boolean isEmail(String value, String msg) {
		if (value == null || value.equals(""))
			return true;
		String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		Pattern p = Pattern.compile(regex);
		if (p.matcher(value).matches()) {
			return true;
		} else {
			throw new ValidationException(msg);
		}
	}

	/**
	 * 验证是否为IP地址
	 * 
	 * @param value
	 * @return
	 * @throws ValidationException
	 */
	public static boolean isIpAddress(String value) {
		return isIpAddress(value, "IP地址格式不正确");
	}

	/**
	 * 验证是否为IP地址
	 * 
	 * @param value
	 * @return
	 * @throws ValidationException
	 */
	public static boolean isIpAddress(String value, String msg) {
		String regex = "(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)";
		Pattern p = Pattern.compile(regex);
		if (p.matcher(value).matches()) {
			return true;
		} else {
			throw new ValidationException(msg);
		}
	}

	/**
	 * 验证是否为中国邮政编码 注意：为null或字符串""都返回true
	 * 
	 * @param value
	 * @return
	 * @throws ValidationException
	 */
	public static boolean isPostCode(String value) {
		return isPostCode(value, "邮政编码无效");
	}

	/**
	 * 验证是否为中国邮政编码 注意：为null或字符串""都返回true
	 * 
	 * @param value
	 * @return
	 * @throws ValidationException
	 */
	public static boolean isPostCode(String value, String msg) {
		if (value == null || value.equals(""))
			return true;
		String regex = "(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)";
		Pattern p = Pattern.compile(regex);
		if (p.matcher(value).matches()) {
			return true;
		} else {
			throw new ValidationException(msg);
		}
	}

	/**
	 * 验证是否为中国身份证
	 * 
	 * @param value
	 * @return
	 * @throws ValidationException
	 */
	public static boolean isIdentity(String value) {
		return isIdentity(value, "身份证号码无效");
	}

	/**
	 * 验证是否为中国身份证
	 * 
	 * @param value
	 * @return
	 * @throws ValidationException
	 */
	public static boolean isIdentity(String value, String msg) {
		String regex = "(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)";
		Pattern p = Pattern.compile(regex);
		if (p.matcher(value).matches()) {
			return true;
		} else {
			throw new ValidationException(msg);
		}
	}

	/**
	 * 验证是变量是否为空
	 * 
	 * @param value
	 * @return
	 * @throws ValidationException
	 */
	public static boolean isNullOrBlank(Object value, String msg) {
		if (value == null || value.toString().equals(""))
			throw new ValidationException(msg);
		return true;
	}

}
